package system_code.CopyFile;

import java.io.*;
import java.util.Scanner;

public class Test4 {
    public static void main(String[] args) {
        //输入信息
        System.out.print("请输入您要复制的文件名:");
        Scanner in = new Scanner(System.in);
        File srcFile = new File(in.next());
        if(!srcFile.isFile()) {
            System.out.println("输入有误这不是一个有效文件!");
            return;
        }
        System.out.print("请输入您要复制到哪个文件下:");
        File destFile = new File(in.next());
        if(!destFile.isFile()) {
            System.out.println("目标文件输入有误!");
            return;
        }
        //打开源文件，将内容读取出并放入目标文件中
        try(OutputStream outputStream = new FileOutputStream(destFile);
            InputStream inputStream = new FileInputStream(srcFile)) {
            //通过字节流读取内容
            int count = 0;//记录多少字节
            while(true) {
                int ret = inputStream.read();
                if(ret == -1) {
                    System.out.println("文件复制成功，共复制了" + count + "字节数据");
                    break;
                }
                outputStream.write(ret);
                count++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
